/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.engagemangsindex.task;

import java.net.MalformedURLException;
import java.net.URL;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.xml.ws.BindingProvider;
import lombok.extern.slf4j.Slf4j;
import riv.itintegration.registry._1.ServiceContractNamespaceType;
import riv.itintegration.registry.getlogicaladdresseesbyservicecontract._1.rivtabp21.GetLogicalAddresseesByServiceContractResponderInterface;
import riv.itintegration.registry.getlogicaladdresseesbyservicecontract._1.rivtabp21.GetLogicalAddresseesByServiceContractResponderService;
import riv.itintegration.registry.getlogicaladdresseesbyservicecontractresponder._1.GetLogicalAddresseesByServiceContractResponseType;
import riv.itintegration.registry.getlogicaladdresseesbyservicecontractresponder._1.GetLogicalAddresseesByServiceContractType;
import se.sll.engagemangsindex.ejb.ConfigurationHolder;
import se.sll.engagemangsindex.ejb.LogicalAddressCache;

/**
 *
 * @author khaleddaham
 */
@Stateless
@Slf4j
public class LogicalAddressesTask {

    @Inject
    private LogicalAddressCache logicalCache;
    @Inject
    private ConfigurationHolder configuration;
    private BindingProvider logicalAddresseesBindingProvider = null;
    private GetLogicalAddresseesByServiceContractResponderInterface addresseesByContractPort = null;
    private String contract = "urn:riv:itintegration:engagementindex:ReceiveNotification:1";

    /**
     * Setup service
     */
    public LogicalAddressesTask() throws MalformedURLException {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        if (null == cl) {
            cl = LogicalAddressesTask.class.getClassLoader();
        }
        URL wsdlLocation = cl.getResource("META-INF/GetLogicalAddresseesByServiceContractInteraction_1.0_RIVTABP21.wsdl");

        GetLogicalAddresseesByServiceContractResponderService logicalAddresseesByServiceContract = new GetLogicalAddresseesByServiceContractResponderService(
                wsdlLocation);
        addresseesByContractPort = logicalAddresseesByServiceContract.getGetLogicalAddresseesByServiceContractResponderPort();
        logicalAddresseesBindingProvider = (BindingProvider) addresseesByContractPort;
    }

    /**
     * @TODO filter contracts so we only get the ones we must have
     *
     * @param endpointURL
     */
    public void cacheLogicalAddresses(String endpointURL) {
        GetLogicalAddresseesByServiceContractType parameters = new GetLogicalAddresseesByServiceContractType();
        ServiceContractNamespaceType contractNamespaceType = new ServiceContractNamespaceType();
        contractNamespaceType.setServiceContractNamespace(this.contract);
        parameters.setServiceContractNameSpace(contractNamespaceType);
        parameters.setServiceConsumerHsaId(configuration.getOwner());

        logicalAddresseesBindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
        try {
            GetLogicalAddresseesByServiceContractResponseType response = addresseesByContractPort.getLogicalAddresseesByServiceContract(configuration.getServiceCatalogue(), parameters);
            if (response.getLogicalAddress() != null) {
                logicalCache.updateCache(response.getLogicalAddress());
            }
        } catch (Exception ex) {
            log.warn("Unable to update logical adresses!", ex);            
        }
    }

    /**
     *
     * @param cache
     */
    protected void setLogicalAddressCache(LogicalAddressCache cache) {
        this.logicalCache = cache;
    }

    void setConfigurationHolder(ConfigurationHolder holder) {
        this.configuration = holder;
    }

    void setBindingProvider(BindingProvider mockBindingProvider) {
        this.logicalAddresseesBindingProvider = mockBindingProvider;
    }

    void setGetLogicalAddresseesByServiceContractResponderInterface(GetLogicalAddresseesByServiceContractResponderInterface responderInterface) {
        this.addresseesByContractPort = responderInterface;
    }
}